import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

const store = new Vuex.Store({
	state: {
		forcedLogin: true,
		hasLogin: false,
		userName: uni.getStorageSync('user') ? uni.getStorageSync('user').user.username : null,
		tabindex: 0,
		userInfo: uni.getStorageSync('user') ? uni.getStorageSync('user') : null,
		proID:'',
		userID: '',
		reportValue:'',
		IP: null,
		pageInfo: uni.getStorageSync('pageInfo') ? uni.getStorageSync('pageInfo') : [],
		info: uni.getStorageSync('errorInfo') ? uni.getStorageSync('errorInfo') : null,
	},
	mutations: {
		login(state, data) {
			state.userInfo = data.data
			state.userID = data.data.user.id
			state.hasLogin = true;
			state.userName = data.data.user.username
			state.pageInfo = data.data.pageList
			uni.setStorageSync('pageInfo', data.data.pageList)
			uni.setStorageSync('user', data.data)
			uni.setStorageSync('userID', state.userID)
			uni.setStorageSync('hasLogin', state.hasLogin)
			uni.setStorageSync('factoryId', data.data.user.factoryId)
		},
		logout(state) {
			state.userName = null;
			state.hasLogin = false;
			state.IP = null
			uni.removeStorageSync('user')
			uni.removeStorageSync('pageInfo')
			uni.removeStorageSync('hasLogin')
			uni.removeStorageSync('factoryId')
		},
		increment (state, n) {
		   state.reportValue = n
		},
		saveInfo(state, data) {
			data.flag = true // 判断消息是否已读
			state.info = data
			uni.setStorageSync('errorInfo', data)
		},
		changeInfo(state, flag) {
			state.info.flag = flag
			uni.setStorageSync('errorInfo', state.info)
		}
	},
	actions: {
		changeInfoTap(context, data) {
			context.commit('changeInfo', false)
		}
	}
})

export default store
